﻿
using DualPlaneFR4Paster.Common.Services;
using DualPlaneFR4Paster.Extensions;
using DualPlaneFR4Paster.Models;
using Prism.Commands;
using Prism.Ioc;
using Prism.Services.Dialogs;
using System.Collections.ObjectModel;
using System.Linq;

namespace DualPlaneFR4Paster.ViewModels.Dialogs
{
    public class PartNumDialogViewModel : DialogViewModel
    {
        #region 变量
        private IParamService paramService;
        #endregion
        #region 属性绑定
        private string currentPartNum;
        public string CurrentPartNum
        {
            get { return currentPartNum; }
            set { SetProperty(ref currentPartNum, value); }
        }
        private ObservableCollection<MyPartNum> partNumbList;
        public ObservableCollection<MyPartNum> PartNumbList
        {
            get { return partNumbList; }
            set { SetProperty(ref partNumbList, value); }
        }
        #endregion
        #region 方法绑定
        private DelegateCommand addCommand;
        public DelegateCommand AddCommand =>
            addCommand ?? (addCommand = new DelegateCommand(ExecuteAddCommand));
        private DelegateCommand<MyPartNum> deleteCommand;
        public DelegateCommand<MyPartNum> DeleteCommand =>
            deleteCommand ?? (deleteCommand = new DelegateCommand<MyPartNum>(ExecuteDeleteCommand));
        private DelegateCommand<MyPartNum> selectCommand;
        public DelegateCommand<MyPartNum> SelectCommand =>
            selectCommand ?? (selectCommand = new DelegateCommand<MyPartNum>(ExecuteSelectCommand));

        void ExecuteSelectCommand(MyPartNum partNum)
        {
            if (partNum != null)
            {
                if (partNum.Name != CurrentPartNum)
                {
                    var result = _dialogHostService.MessageBox(MessageType.Confirm, "信息", $"确认切换为\"{partNum.Name}\"料号吗？");
                    if (result == ButtonResult.Yes)
                    {
                        var result1 = paramService.ChangePart(partNum.Name);
                        if (result1)
                        {
                            CurrentPartNum = paramService.GetPart();
                        }
                    }
                }
            }
            else
            {
                _dialogHostService.MessageBox(MessageType.Message, "信息", $"未选择任何料号");
            }
        }
        void ExecuteDeleteCommand(MyPartNum partNum)
        {
            if (partNum != null)
            {
                if (partNum.Name != CurrentPartNum)
                {
                    var result = _dialogHostService.MessageBox(MessageType.Confirm, "信息", $"确定删除料号\"{partNum.Name}\"吗?");
                    if (result == ButtonResult.Yes)
                    {
                        var result1 = paramService.DeletPart(partNum.Name);
                        var allParts = paramService.GetAllParts();
                        PartNumbList.Clear();
                        for (int i = 0; i < allParts.Count; i++)
                        {
                            PartNumbList.Add(new MyPartNum
                            {
                                Id = i + 1,
                                Name = allParts[i]
                            });
                        }
                        if (!result1)
                        {
                            _dialogHostService.MessageBox(MessageType.Message, "信息", $"删除失败");
                        }
                    }
                }
                else
                {
                    _dialogHostService.MessageBox(MessageType.Message, "信息", $"料号\"{partNum.Name}\"正在使用，不能删除");
                }
            }
            else
            {
                _dialogHostService.MessageBox(MessageType.Message, "信息", $"未选择任何料号");
            }
        }
        void ExecuteAddCommand()
        {
            dialogService.ShowDialog("AddNewPartNumDialog",arg =>
            {
                if (arg.Result == ButtonResult.Yes)
                {
                    var newpartName = arg.Parameters.GetValue<string>("NewPartNumName");
                    if (newpartName != string.Empty)
                    {
                        var _part = PartNumbList.FirstOrDefault(p => p.Name == newpartName);
                        if (_part == null)
                        {
                            var result = paramService.AddPart(newpartName);
                            var allParts = paramService.GetAllParts();
                            PartNumbList.Clear();
                            for (int i = 0; i < allParts.Count; i++)
                            {
                                PartNumbList.Add(new MyPartNum
                                {
                                    Id = i + 1,
                                    Name = allParts[i]
                                });
                            }
                            if (!result)
                            {
                                _dialogHostService.MessageBox(MessageType.Message, "信息", "添加失败");
                            }
                        }
                        else
                        {
                            _dialogHostService.MessageBox(MessageType.Message, "信息", $"料号\"{newpartName}\"已存在");
                        }
                    }
                    else
                    {
                        _dialogHostService.MessageBox(MessageType.Message, "信息", "料号名不能为空");
                    }
                }
            });
        }
        #endregion
        #region 导航
        public override void OnDialogOpened(IDialogParameters parameters)
        {
            CurrentPartNum = paramService.GetPart();
            var allParts = paramService.GetAllParts();
            for (int i = 0; i < allParts.Count; i++)
            {
                PartNumbList.Add(new MyPartNum { 
                    Id = i + 1,
                    Name = allParts[i]
                });
            }
        }
        #endregion
        public PartNumDialogViewModel(IContainerProvider containerProvider) : base(containerProvider)
        {
            paramService = containerProvider.Resolve<IParamService>("Param");
            PartNumbList = new ObservableCollection<MyPartNum>();
        }
    }
    public class MyPartNum
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
}
